package org.sbbs.base.webapp.filter;

import java.io.IOException;
import java.util.Locale;
import java.util.Map;

import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.servlet.jsp.jstl.core.Config;

import org.apache.struts2.dispatcher.Dispatcher;
import org.sbbs.base.Constants;
import org.springframework.context.i18n.LocaleContextHolder;
import org.springframework.web.filter.OncePerRequestFilter;

import com.opensymphony.xwork2.config.Configuration;
import com.opensymphony.xwork2.config.ConfigurationManager;
import com.opensymphony.xwork2.config.RuntimeConfiguration;
import com.opensymphony.xwork2.config.entities.ActionConfig;

/**
 * Filter to wrap request with a request including user preferred locale.
 */
public class LocaleFilter extends OncePerRequestFilter {

	/**
	 * This method looks for a "locale" request parameter. If it finds one,
	 * it sets it as the preferred locale and also configures it to work
	 * with JSTL.
	 * 
	 * @param request
	 *                the current request
	 * @param response
	 *                the current response
	 * @param chain
	 *                the chain
	 * @throws IOException
	 *                 when something goes wrong
	 * @throws ServletException
	 *                 when a communication failure happens
	 */
	@SuppressWarnings("unchecked")
	public void doFilterInternal(HttpServletRequest request,
			HttpServletResponse response, FilterChain chain)
			throws IOException, ServletException {

		String locale = request.getParameter("locale");
		Locale preferredLocale = null;

		if (locale != null) {
			int indexOfUnderscore = locale.indexOf('_');
			if (indexOfUnderscore != -1) {
				String language = locale.substring(0, indexOfUnderscore);
				String country = locale.substring(indexOfUnderscore + 1);
				preferredLocale = new Locale(language, country);
			} else {
				preferredLocale = new Locale(locale);
			}
		}

		HttpSession session = request.getSession(true);

		if (session != null) {
			if (preferredLocale == null) {
				preferredLocale = (Locale) session
						.getAttribute(Constants.PREFERRED_LOCALE_KEY);
			} else {
				session.setAttribute(Constants.PREFERRED_LOCALE_KEY,
						preferredLocale);
				Config.set(session, Config.FMT_LOCALE, preferredLocale);
			}

			if (preferredLocale != null
					&& !(request instanceof LocaleRequestWrapper)) {
				request = new LocaleRequestWrapper(request, preferredLocale);
				LocaleContextHolder.setLocale(preferredLocale);
			}
		}

		String theme = request.getParameter("theme");
		if (theme != null) {
			session.setAttribute(Constants.CSS_THEME, theme);
		} else {
			String sTheme = (String) session.getAttribute(Constants.CSS_THEME);
			if (sTheme == null)
				session.setAttribute(Constants.CSS_THEME, "hot-sneaks");
		}

		chain.doFilter(request, response);

		// Reset thread-bound LocaleContext.
		LocaleContextHolder.setLocaleContext(null);
		// this.printAllStruts2Actions();
	}

	private void printAllStruts2Actions() {
		Dispatcher dispatcher = Dispatcher.getInstance();

		ConfigurationManager cm = dispatcher.getConfigurationManager();

		Configuration cf = cm.getConfiguration();

		RuntimeConfiguration rc = cf.getRuntimeConfiguration();

		Map<String, Map<String, ActionConfig>> d = rc.getActionConfigs();

		for (String key1 : d.keySet()) {
			System.out.println(key1);
			Map<String, ActionConfig> a = d.get(key1);
			for (String key2 : a.keySet()) {
				System.out.println(key2);
				ActionConfig ac = a.get(key2);
			}
		}
	}
}
